package com.tjn;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class Exercise02 {
    public static void main(String[] args) {
        GetMoney getMoney = new GetMoney();
        Thread thread1 = new Thread(getMoney);
        thread1.setName("张三");
        Thread thread2 = new Thread(getMoney);
        thread2.setName("李四");
        thread1.start();
        thread2.start();
    }
}
class GetMoney implements Runnable{
    private boolean loop = true;
    private int money = 10000;

    public void getMoney()  {
        synchronized (this){
            if(money <= 0){
                System.out.println("没钱了");
                loop = false;
                return;
            }
            System.out.println(Thread.currentThread().getName() + "取走1000元" + "还剩" + (money -= 1000));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void run() {
        while(loop){
            getMoney();
        }
    }
}